package com.xcjh.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.xcjh.base.dto.LoginUser;
import com.xcjh.domain.LoginInfo;
import com.xcjh.utils.RedisUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class AutoFullDataHandler implements MetaObjectHandler {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public void insertFill(MetaObject metaObject) {
        LoginUser<LoginInfo> user=redisUtils.getCurrentUser();
        String username = user==null||user.getUser()==null?"":user.getUser().getUserName();
        this.strictInsertFill(metaObject,"createTime",Date.class,new Date());
        this.strictInsertFill(metaObject, "createBy", String.class,username); // 起始版本 3.3.0(推荐使用)
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LoginUser<LoginInfo> user=redisUtils.getCurrentUser();
        String username = user==null||user.getUser()==null?"":user.getUser().getUserName();

        this.strictUpdateFill(metaObject,"updateTime", Date.class,new Date());
        this.strictUpdateFill(metaObject, "createBy", String.class, username); // 起始版本 3.3.0(推荐使用)
    }
}
